 package com.jc.framework.filter;

import java.io.*;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import com.jc.framework.util.WebConstants;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.web.session.mgt.WebSessionKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class RequestFilter implements Filter {

	private String encodeing;
	private static Logger logger = LoggerFactory.getLogger(RequestFilter.class);
	
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		encodeing = "UTF-8";
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
//		ServletRequest requestWrapper = null;
//        if(request instanceof HttpServletRequest) {
//            requestWrapper = new BodyReaderHttpServletRequestWrapper((HttpServletRequest) request);
//        }
		HttpServletResponse respon = (HttpServletResponse) response;
		respon.setHeader("Access-Control-Allow-Origin", "*");
		respon.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
		respon.setHeader("Access-Control-Max-Age", "3600");
		respon.setHeader("Access-Control-Allow-Headers", "x-requested-with");

		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}
	
	
	
	
	class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {  
		  
	    private byte[] body;  
	    private String encodeing = null;
	    public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {  
	        super(request);  
	        //body = StreamUtil.readBytes(request.getReader(), JoddDefault.encoding);  
	        
	        encodeing = request.getCharacterEncoding();
	        StringBuffer stringBuffer = new StringBuffer();
	        BufferedReader bufferedReader = null;
	        
	        
	        try {
	        	bufferedReader = this.getRequest().getReader();
	        	String val = null;
	        	while((val = bufferedReader.readLine()) != null) {
	        		stringBuffer.append(val);
	        	}
	        	//下面这个UTF-8很重要,如果不加,会导致BODY提交中文的时候无法转换为JSON
	        	body = stringBuffer.toString().getBytes(encodeing);
	        }catch(IOException e) {
	        	logger.error("RequestFilter.BodyReaderHttpServletRequestWrapper异常" , e);
	        }finally {
				try {
					if(bufferedReader != null) {
						bufferedReader.close();
					}
				}catch(Exception e) {}
			}
	        
	        
	    }  
	  
	    @Override  
	    public BufferedReader getReader() throws IOException {  
	        return new BufferedReader(new InputStreamReader(getInputStream()));  
	    }  
	  
	    @Override  
	    public ServletInputStream getInputStream() throws IOException {  
	        final ByteArrayInputStream bais = new ByteArrayInputStream(body);  
	        return new ServletInputStream() {  
	  
	            @Override  
	            public int read() throws IOException {  
	                return bais.read();  
	            }  
	        };  
	    }  
	  
	}
}
